﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace DeleteEmptyFolders
{
	[Serializable]
	public class FolderDeleterSettings
	{
		public string[] IgnoreFiles
		{
			get;
			set;
		}

		public string[] BaseDirs
		{
			get;
			set;
		}

		public string Save()
		{
			string saveFilepath = getDefaultSaveFilePath();
			Save(saveFilepath);
			return saveFilepath;
		}

		private static string getDefaultSaveFilePath()
		{

			string saveFilepath = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "FolderDeleterSettings.xml");
			return saveFilepath;
		}

		private void Save(string saveFilepath)
		{
			using (StreamWriter sw = new StreamWriter(saveFilepath))
			{
				XmlSerializer xs = new XmlSerializer(this.GetType());
				xs.Serialize(sw, this);
			}
		}

		public static FolderDeleterSettings LoadFromDisk()
		{
			return LoadFromDisk(getDefaultSaveFilePath());
		}

		public static FolderDeleterSettings LoadFromDisk(string filePath)
		{
			if (!File.Exists(filePath))
			{
				createDefaultSettings(filePath);
			}
			using (StreamReader sr = new StreamReader(filePath))
			{
				XmlSerializer xs = new XmlSerializer(typeof(FolderDeleterSettings));
				FolderDeleterSettings fds = xs.Deserialize(sr) as FolderDeleterSettings;

				return fds;
			}
		}

		private static void createDefaultSettings(string filePath)
		{
			FolderDeleterSettings fds = new FolderDeleterSettings
			{
				BaseDirs = new string[] { "c:\\TestDir" },
				IgnoreFiles = new string[] { "*.jpg", "*.txt", "*.nfo" }
			};

			fds.Save();
		}
	}
}
